using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Terrain44
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        GraphicsDevice device;

        Texture2D heightMap;
        Texture2D[] terrainTextures;
        Effect effect;

       
        Terrain currentTerrain;


        KeyboardState ks;
        Explosion explosion;
        Texture2D explTexture;
        Texture2D explBlueTexture;
        Texture2D explGreenTexture;
        Texture2D explPurpleTexture;
        Texture2D explYellowTexture;
        Texture2D explRedTexture;
        Texture2D smoke;
        Effect explEffect;

        int particleCount = 500;
        float particleSize = 10.0f;
        float totalSize = 10.0f;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            graphics.PreferredBackBufferWidth = 1200;
            graphics.PreferredBackBufferHeight = 750;
            graphics.IsFullScreen = false;
            graphics.ApplyChanges();


            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            device = graphics.GraphicsDevice;

            effect = Content.Load<Effect>("effects");
            heightMap = Content.Load<Texture2D>("heightmap002");
            //heightMap = Content.Load<Texture2D>("hm01");
            //heightMap = Content.Load<Texture2D>("hm02");
            //heightMap = Content.Load<Texture2D>("hm03");

            LoadTerrainTextures();

            explEffect = Content.Load<Effect>("EffectExplosion");
            explTexture = Content.Load<Texture2D>("explosion");
            explBlueTexture = Content.Load<Texture2D>("explosionBlue");
            explGreenTexture = Content.Load<Texture2D>("explosionGreen");
            explPurpleTexture = Content.Load<Texture2D>("explosionPurple");
            explYellowTexture = Content.Load<Texture2D>("explosionYellow");
            explRedTexture = Content.Load<Texture2D>("explosionRed");
            smoke = Content.Load<Texture2D>("smoke");

            
            currentTerrain = new Terrain(heightMap, terrainTextures, effect, device);

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

           

            currentTerrain.UpdateTerrain();

            ks = Keyboard.GetState();
            if (ks.IsKeyDown(Keys.D1))
            {
                //(position, particleCount, particleSize, explSize,...)
                explosion = new Explosion(new Vector3(0, 100, 50), particleCount, particleSize, totalSize, device, (float)gameTime.TotalGameTime.TotalMilliseconds, new Vector3(0, 0, 0), explEffect, explTexture);
            }

            if (ks.IsKeyDown(Keys.D2))
            {
                //(position, particleCount, particleSize, explSize,...)
                explosion = new Explosion(new Vector3(0, 100, 50), particleCount, particleSize, totalSize, device, (float)gameTime.TotalGameTime.TotalMilliseconds, new Vector3(0, 0, 0), explEffect, explBlueTexture);
            }

            if (ks.IsKeyDown(Keys.D3))
            {
                //(position, particleCount, particleSize, explSize,...)
                explosion = new Explosion(new Vector3(0, 100, 50), particleCount, particleSize, totalSize, device, (float)gameTime.TotalGameTime.TotalMilliseconds, new Vector3(0, 0, 0), explEffect, explGreenTexture);
            }

            if (ks.IsKeyDown(Keys.D4))
            {
                //(position, particleCount, particleSize, explSize,...)
                explosion = new Explosion(new Vector3(0, 100, 50), particleCount, particleSize, totalSize, device, (float)gameTime.TotalGameTime.TotalMilliseconds, new Vector3(0, 0, 0), explEffect, explYellowTexture);
            }

            if (ks.IsKeyDown(Keys.D5))
            {
                //(position, particleCount, particleSize, explSize,...)
                explosion = new Explosion(new Vector3(0, 100, 50), particleCount, particleSize, totalSize, device, (float)gameTime.TotalGameTime.TotalMilliseconds, new Vector3(0, 0, 0), explEffect, explRedTexture);
            }

            if (ks.IsKeyDown(Keys.D6))
            {
                //(position, particleCount, particleSize, explSize,...)
                explosion = new Explosion(new Vector3(0, 100, 50), particleCount, particleSize, totalSize, device, (float)gameTime.TotalGameTime.TotalMilliseconds, new Vector3(0, 0, 0), explEffect, explPurpleTexture);
            }

            if (ks.IsKeyDown(Keys.D7))
            {
                //(position, particleCount, particleSize, explSize,...)
                explosion = new Explosion(new Vector3(0, 100, 50), particleCount, particleSize, totalSize, device, (float)gameTime.TotalGameTime.TotalMilliseconds, new Vector3(0, 0, 0), explEffect, smoke);
            }


            if (explosion != null)
            {
                explosion.UpdateExplosion(gameTime);
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);

            // TODO: Add your drawing code here

            currentTerrain.DrawTerrain();
            if (explosion != null)
            {
               
                    explosion.Draw(gameTime);
            }
            base.Draw(gameTime);
        }


        private void LoadTerrainTextures()
        {
            terrainTextures = new Texture2D[12];
            terrainTextures[0] = Content.Load<Texture2D>("terrainTextures/lavaTexture_1");
            terrainTextures[1] = Content.Load<Texture2D>("terrainTextures/lavaTexture_2");
            terrainTextures[2] = Content.Load<Texture2D>("terrainTextures/lavaTexture_3");
            terrainTextures[3] = Content.Load<Texture2D>("terrainTextures/lavaTexture_4");
            terrainTextures[4] = Content.Load<Texture2D>("terrainTextures/moonTexture_1");
            terrainTextures[5] = Content.Load<Texture2D>("terrainTextures/moonTexture_2");
            terrainTextures[6] = Content.Load<Texture2D>("terrainTextures/moonTexture_3");
            terrainTextures[7] = Content.Load<Texture2D>("terrainTextures/moonTexture_4");
            terrainTextures[8] = Content.Load<Texture2D>("terrainTextures/sandTexture_1");
            terrainTextures[9] = Content.Load<Texture2D>("terrainTextures/sandTexture_2");
            terrainTextures[10] = Content.Load<Texture2D>("terrainTextures/sandTexture_3");
            terrainTextures[11] = Content.Load<Texture2D>("terrainTextures/sandTexture_4");

        }
    }
}
